import { h } from 'vue';
import { FormItemRule, NSwitch } from 'naive-ui';
import { setRoleStatus } from '@/api/system';
import { FormSchema } from '@/components/Form';
import { truncate } from 'fs/promises';
const $message = window['$message'];
export const columns: any[] = [
    {
        type: 'selection',
        fixed: 'left',
    },
	{
		title: '账号',
		key: 'account',
        width: 100,
	},
    {
		title: '是否可用',
		key: 'status',
        width: 100,
		render: (record) => {
			return h(NSwitch, {
				value: record.status === '1',
				loading: !!record.loading,
				size: 'large',
				onUpdateValue(checked: boolean) {
					record.loading = true;
					const newStatus = checked ? '1' : '0';
					setRoleStatus(record.id, newStatus)
						.then(() => {
							record.status = newStatus;
							$message.success(`已成功修改用户状态`);
						})
						.catch((err) => {
							$message.error('修改用户状态失败');
						})
						.finally(() => {
							record.loading = false;
						});
				},
			});
		},
	},
    {
		title: '姓名',
		key: 'realName',
        width: 100,
	},
	{
		title: '电话',
		key: 'phone',
        width: 100,
	},
    {
		title: '角色',
		key: 'role',
        width: 100,
	},
	{
		title: '电子邮箱',
		key: 'email',
        width: 100,
	},
    
];

export const formSchema: FormSchema[] = [
	{
		field: 'account',
		label: '账号',
		component: 'NInput',
		rules: [{ required: true, message: '请输入账号', trigger: 'blur' }],
	},
    {
		field: 'role',
		label: '角色',
		component: 'NSelect',
		defaultValue: '0',
		componentProps: {
			multiple:true,
			options: [
				{ label: '超级管理员', value: '0' },
				{ label: '管理员', value: '1' },
				{ label: '文章管理员', value: '2' },
				{ label: '普通用户', value: '3' },
			],
		},
	},
	{
		field: 'status',
		label: '状态',
		component: 'NRadioGroup',
		defaultValue: '0',
		componentProps: {
			options: [
				{ label: '启用', value: '0' },
				{ label: '停用', value: '1' },
			],
		},
	},
    {
		field: 'realName',
		label: '姓名',
		component: 'NInput',
        rules: [{ max: 20, message: '超出长度', trigger: 'input' }],
	},
    {
		field: 'phone',
		label: '电话',
		component: 'NInput',
        // rules:[
        //     {
        //         trigger: ['input'],
        //         message: '请输入正确电话格式',
        //         validator: (rule: FormItemRule, value: string) => {
        //           return /^[1]+\\d{10}$/.test(value)
        //         }
        //       }
        // ]
	},
    {
		field: 'email',
		label: '电子邮箱',
		component: 'NInput',
	},
	{
		label: '备注',
		field: 'remark',
		component: 'NInput',
		componentProps: {
			type: 'textarea',
		},
	},
	
];
